%%Difference plotter for NW and EW decoding probabilities
clear
clc
close all
%%
y_lim_min =-13
y_lim_max = 13

y_ticks = 2;
y_max = y_lim_max;
y_min = y_lim_min;


num_sim = 10000

%%
%EW
%Load anal data

L1_g_30_anal_ew = load('/Users/benjamin/Documents/AAU/6.semester/P6trunk/plotting/uep_analytic/uep_ew_analytic_g1_03_g2_07.mat','l1_prob_1');
L2_g_30_anal_ew = load('/Users/benjamin/Documents/AAU/6.semester/P6trunk/plotting/uep_analytic/uep_ew_analytic_g1_03_g2_07.mat','l2_prob_1');
L1_g_30_anal_ew = L1_g_30_anal_ew.l1_prob_1;
L2_g_30_anal_ew = L2_g_30_anal_ew.l2_prob_1;


L1_g_40_anal_ew = load('/Users/benjamin/Documents/AAU/6.semester/P6trunk/plotting/uep_analytic/uep_ew_analytic_g1_04_g2_06.mat','l1_prob_1');
L2_g_40_anal_ew = load('/Users/benjamin/Documents/AAU/6.semester/P6trunk/plotting/uep_analytic/uep_ew_analytic_g1_04_g2_06.mat','l2_prob_1');
L1_g_40_anal_ew = L1_g_40_anal_ew.l1_prob_1;
L2_g_40_anal_ew = L2_g_40_anal_ew.l2_prob_1;

L1_g_50_anal_ew = load('/Users/benjamin/Documents/AAU/6.semester/P6trunk/plotting/uep_analytic/uep_ew_analytic_g1_05_g2_05.mat','l1_prob_1');
L2_g_50_anal_ew = load('/Users/benjamin/Documents/AAU/6.semester/P6trunk/plotting/uep_analytic/uep_ew_analytic_g1_05_g2_05.mat','l2_prob_1');
L1_g_50_anal_ew = L1_g_50_anal_ew.l1_prob_1;
L2_g_50_anal_ew = L2_g_50_anal_ew.l2_prob_1;


%Load sim data
%L1_30_sim_ew = load('data_points_L1_EW_g130.txt')/num_sim;
% L1_50_sim_ew = load('data_points_L1_EW_g150.txt')/num_sim;
% L1_40_sim_ew = load('data_points_L1_EW_g140.txt')/num_sim;
% %L2_30_sim_ew = load('data_points_L2_EW_g130.txt')/num_sim;
% L2_50_sim_ew = load('data_points_L2_EW_g150.txt')/num_sim;
% L2_40_sim_ew = load('data_points_L2_EW_g140.txt')/num_sim;

%%
Start = 40;
Slut = 196;

%% UEP gamma 30
uep = load('uepRandom30.txt');

Total = max(uep);
Total = Total(2);

Layer = zeros(2,Total);
LayerG = ones(2,1)*-1;
Layers = 2;

for G=1:size(uep,1)
    for L=1:Layers
        if LayerG(L) ~= uep(G,1) && uep(G,L+2) == 1
%            LayerG(L) = uep(G,1);
%            Layer(L,uep(G,2):Total) = Layer(L,uep(G,2):Total) + 1;
            for i=1:L
                if LayerG(i) ~= uep(G,1)
                    LayerG(i) = uep(G,1);
                    Layer(i,uep(G,2):Total) = Layer(i,uep(G,2):Total) + 1;
                end
            end
        end
    end
end

Range = Total;
UEP_Res_X = 1:Slut;
UEP_Res_X_Short = 1:3:Slut;
Top=Layer(2,Total);
UEP_Res_1 = [Layer(1,1:Total)/Top ones(1,Slut-Total)];
UEP_Res_2 = [Layer(2,1:Total)/Top ones(1,Slut-Total)];
UEP_Res_1_Short = UEP_Res_1(UEP_Res_X_Short);
UEP_Res_2_Short = UEP_Res_2(UEP_Res_X_Short);

L1_30_sim_ew=UEP_Res_1;
L2_30_sim_ew=UEP_Res_2;


%% UEP gamma 40
uep = load('uepRandom40.txt');

Total = max(uep);
Total = Total(2);

Layer = zeros(2,Total);
LayerG = ones(2,1)*-1;
Layers = 2;

for G=1:size(uep,1)
    for L=1:Layers
        if LayerG(L) ~= uep(G,1) && uep(G,L+2) == 1
%            LayerG(L) = uep(G,1);
%            Layer(L,uep(G,2):Total) = Layer(L,uep(G,2):Total) + 1;
            for i=1:L
                if LayerG(i) ~= uep(G,1)
                    LayerG(i) = uep(G,1);
                    Layer(i,uep(G,2):Total) = Layer(i,uep(G,2):Total) + 1;
                end
            end
        end
    end
end

Range = Total;
UEP_Res_X = 1:Slut;
UEP_Res_X_Short = 1:3:Slut;
Top=Layer(2,Total);
UEP_Res_1 = [Layer(1,1:Total)/Top ones(1,Slut-Total)];
UEP_Res_2 = [Layer(2,1:Total)/Top ones(1,Slut-Total)];
UEP_Res_1_Short = UEP_Res_1(UEP_Res_X_Short);
UEP_Res_2_Short = UEP_Res_2(UEP_Res_X_Short);

L1_40_sim_ew=UEP_Res_1;
L2_40_sim_ew=UEP_Res_2;

%% UEP gamma 50
uep = load('uepRandom50.txt');

Total = max(uep);
Total = Total(2);

Layer = zeros(2,Total);
LayerG = ones(2,1)*-1;
Layers = 2;

for G=1:size(uep,1)
    for L=1:Layers
        if LayerG(L) ~= uep(G,1) && uep(G,L+2) == 1
%            LayerG(L) = uep(G,1);
%            Layer(L,uep(G,2):Total) = Layer(L,uep(G,2):Total) + 1;
            for i=1:L
                if LayerG(i) ~= uep(G,1)
                    LayerG(i) = uep(G,1);
                    Layer(i,uep(G,2):Total) = Layer(i,uep(G,2):Total) + 1;
                end
            end
        end
    end
end

Range = Total;
UEP_Res_X = 1:Slut;
UEP_Res_X_Short = 1:3:Slut;
Top=Layer(2,Total);
UEP_Res_1 = [Layer(1,1:Total)/Top ones(1,Slut-Total)];
UEP_Res_2 = [Layer(2,1:Total)/Top ones(1,Slut-Total)];
UEP_Res_1_Short = UEP_Res_1(UEP_Res_X_Short);
UEP_Res_2_Short = UEP_Res_2(UEP_Res_X_Short);

L1_50_sim_ew=UEP_Res_1;
L2_50_sim_ew=UEP_Res_2;

%%
%%EW figure
fig1_ew = figure;
hold on
plot((transpose(L1_g_30_anal_ew(1:185))-L1_30_sim_ew(1:185))*100,'k')
plot((transpose(L1_g_40_anal_ew(1:185))-L1_40_sim_ew(1:185))*100,'--k')
plot(-100,-100,'-ok','MarkerSize',3.5);
plot((transpose(L1_g_50_anal_ew(1:185))-L1_50_sim_ew(1:185))*100,'-k')

%Business

label_1 = 'L1, \Gamma_1 0.3,EW';
label_2 = 'L1, \Gamma_1 0.4,EW';
label_3 = 'L1, \Gamma_1 0.5,EW';

legend_h = legend('','','');

set(legend_h,'String',[label_1;label_2;label_3],'location','SouthEast')
set(legend_h,'interpreter','tex');
xlabel('Total number of received packets [-]');
ylabel('Difference in decoding probability [pp]');
set(gca,'XTick',40:10:185,'xlim',[40 185])
set(gca,'YTick',y_min:y_ticks:y_max,'ylim',[y_lim_min y_lim_max])

plot(1:3:185,(transpose(L1_g_50_anal_ew(1:3:185))-L1_50_sim_ew(1:3:185))*100,'ok','MarkerSize',3.5);

grid()
pbaspect([2.5 1 1])
% Save plot
figname=strcat('l1_ew_dif_prac.eps');
print(gcf,'-depsc2',figname)
hold off











%%%%L2 - EW


fig2_ew = figure;
hold on
plot((transpose(L2_g_30_anal_ew(1:185))-L2_30_sim_ew(1:185))*100,'k')
plot((transpose(L2_g_40_anal_ew(1:185))-L2_40_sim_ew(1:185))*100,'--k')
plot(-100,-100,'-ok','MarkerSize',3.5);
plot((transpose(L2_g_50_anal_ew(1:185))-L2_50_sim_ew(1:185))*100,'-k')
plot(1:3:185,(transpose(L2_g_50_anal_ew(1:3:185))-L2_50_sim_ew(1:3:185))*100,'ok','MarkerSize',3.5);


%Business

label_1 = 'L2, \Gamma_1 0.3,EW';
label_2 = 'L2, \Gamma_1 0.4,EW';
label_3 = 'L2, \Gamma_1 0.5,EW';

legend_h = legend('','','');

set(legend_h,'String',[label_1;label_2;label_3],'location','SouthEast')
set(legend_h,'interpreter','tex');
xlabel('Total number of received packets [-]');
ylabel('Difference in decoding probability [pp]');
set(gca,'XTick',40:10:185,'xlim',[40 185])
set(gca,'YTick',y_min:y_ticks:y_max,'ylim',[y_lim_min y_lim_max])


grid()
pbaspect([2.5 1 1])
% Save plot
figname=strcat('l2_ew_dif_prac.eps');
print(gcf,'-depsc2',figname)

hold off
